<template>
  <div class="myContainer">
    <el-row class="myTitleBorderBottom">
      <el-col :span="12" class="myLineHeight_40px">{{this.title}}</el-col>
      <el-col :span="12" class="myTextAlignRight">
        <el-button type="primary" class="myBgGreen" @click="onAdd" v-if="isShow(201)">新 增</el-button>
      </el-col>
    </el-row>
    <el-row class="myMarginTop_10px" :gutter="10">
      <el-col :span="8">
        <el-date-picker
          v-model="time"
          type="daterange"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-col>
      <el-col :span="4">
        <template>
          <el-select 
            v-model="deleteSign"
            clearable
            placeholder="请选择刪除标志">
            <el-option
              v-for="item in deleteSignList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-col>
      <el-col :span="4">
        <el-input placeholder="请输入关键词" v-model="kw"></el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" class="myBgGreen" @click="onSearch">查 询</el-button>
      </el-col>
    </el-row>
    <el-row class="myMarginTop_10px">
      <el-col :span="24">
        <el-table
          ref="multipleTable"
          v-loading="loading"
          :data="tableData"
          :border = true
          :stripe = true
          style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
            label="序号"
            type="index"
            width="100">
          </el-table-column>
          <el-table-column
            label="排序(升序)"
            prop="ORDERBY"
            width="100">
          </el-table-column>
          <el-table-column
            label="商品类型"
            >
            <template scope="scope">
              {{scope.row.GOODSTYPE | goodsType}}
            </template>
          </el-table-column>
          <el-table-column
            prop="GOODSNAME"
            label="商品名称"
            >
          </el-table-column> 
          <el-table-column
            prop="PROMOTIONPRICE"
            label="促销价格"
            >
          </el-table-column> 
          <el-table-column
            prop="LOGOIMG"
            label="商品图片"
            >
            <template scope="scope">
              <a :href='EBIZ_SHOW_PIC_API+scope.row.LOGOIMG' target="_blank">
                <img :src='EBIZ_SHOW_PIC_API+scope.row.LOGOIMG' width="50" height="50"/>
              </a>
            </template>
          </el-table-column>
          <el-table-column
            label="促销库存"
            prop="AMOUNT"
            >
          </el-table-column>
          <el-table-column
            label="已售数量"
            prop="SOLDCOUNT"
            >
          </el-table-column>
          <el-table-column
            label="每人限购数量"
            prop="SINGLEIDMAX"
            >
          </el-table-column>
          <el-table-column
            label="开始时间"
            prop="BEGINTIME"
            >
          </el-table-column>
          <el-table-column
            label="结束时间"
            prop="ENDTIME"
            >
          </el-table-column>
          <el-table-column
            label="促销标志"
            >
            <template scope="scope">
              <span v-if="scope.row.ISSALE==0">未促销</span>
              <span v-else>已促销</span>
            </template>
          </el-table-column>
          <el-table-column
            label="删除标记"
            >
            <template scope="scope">
              <span v-if="scope.row.DELETED==0">未删除</span>
              <span v-else class="myColorRed">已删除</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="100">
            <template scope="scope">
              <el-button type="text primary" @click="modify(scope.row.PROMOTION_ID)" class="look" size="small" v-if="isShow(202)">编辑</el-button> 
              <span v-if="scope.row.DELETED==0">
              | <el-button type="text warning" @click="onDelete(scope.row.PROMOTION_ID)" class="look" size="small" v-if="isShow(203)">删除</el-button>
              </span>
            </template>
          </el-table-column>
        </el-table>

        <div class="block">                    
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page_num"
            :page-sizes="[10, 15]"
            :page-size="page_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped>
  .myContainer{
    background: #fff;
    padding: 10px;
  }
  .myTitleBorderBottom{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    border-bottom: 1px solid #dbdbdb;
    padding-bottom: 5px;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top:2px;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myMarginTop_10px{
    margin-top: 10px;
  }
  .myColorRed{
    color: #f00;
  }
</style>

<script>
  import { getPromotionList , adType , useAd , deleteGoods } from '@/api/online';
  import qs from "querystring";
  import { EBIZ_SHOW_PIC_API } from '@/config/baseUrl';

  export default {
    data(){
      return{  
        title:"超实惠商品管理",    
        tableData:[],  
        total:null, //总条数  
        page_num: 1, //当前页码
        page_size: 10, //每页显示多少条 
        kw:"",
        startTime:"",
        endTime:"",
        deleteSign:"",
        deleteSignList:[{value:0,label:"未删除"},{value:1,label:"已删除"}],
        loading:false,
        time:[],
        EBIZ_SHOW_PIC_API:EBIZ_SHOW_PIC_API
      }
    },
    created() {
      document.title=this.title;
      this.onGetPromotionList();
    },

    filters: {
      goodsType(val){
        return val==1?"超市商品":"餐饮商品";
      }
    },
    methods:{
       onGetPromotionList() {//拉取促销商品列表
        let vm = this;
        if(vm.time){
          vm.startTime=this.time[0];
          vm.endTime=this.time[1];
        }else{
          vm.startTime="";
          vm.endTime="";
        }
        const params = {
          page: vm.page_num,
          rows: vm.page_size,
          GOODSNAME:encodeURI(vm.kw),
          DELETED:vm.deleteSign,
          start:vm.startTime,
          end:vm.endTime
        }
        vm.loading=true;
        getPromotionList(params)
        .then(response => {
          let res = response.data;
          vm.tableData = res.rows;
          vm.total = res.total;
          vm.loading=false;
        })
        .catch(error => {
          vm.loading=false;
          console.log("拉取列表失败",error);
        });
      },
      onSearch() {//查询
        this.onGetPromotionList();
      },
      handleSizeChange(val) {
        this.page_size = val;
        this.onGetPromotionList();
      },
      handleCurrentChange(val) {
        this.page_num = val;
        this.onGetPromotionList();
      },
      handleSelectionChange(val) {
      },
      onAdd(){//新增
        this.$router.push({
          path:"/promotionComponents",
          query: {type: 'add'}
        })
      },
      modify(ad_id){//编辑
        this.$router.push({
          path:"/promotionComponents",
          query: {type: 'update',id:ad_id}
        })
      },
      onDelete(p_ID){//删除
        let vm=this;
        vm.$confirm('您确定要删除这条数据吗？', '提示', {//弹出框
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params={
            PROMOTION_ID:p_ID
          }
          deleteGoods(qs.stringify(params)).then(response => {//逻辑删除商品
            vm.onGetPromotionList();
            vm.$message({
              type: 'success',
              message: '执行成功!'
            });
          }).catch(error => {
            vm.$message.error("执行失败！");
          })
        }).catch(() => {
          vm.$message({
            type: 'info',
            message: '已取消执行'
          });          
        });
      }
    }

  }


</script>



